package com.wms_backend.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wms_backend.dao.FloorDao;
import com.wms_backend.dao.InboundDao;
import com.wms_backend.dao.ShelfDao;
import com.wms_backend.entity.Floor;
import com.wms_backend.entity.Inbound;
import com.wms_backend.entity.Shelf;
import com.wms_backend.entity.Vehicle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

@Component
public class InventoryWebSocketHandler extends TextWebSocketHandler {

    @Autowired
    FloorDao floorDao;

    @Autowired
    ShelfDao shelfDao;

    @Autowired
    InboundDao inboundDao;

    private static final ConcurrentMap<String, WebSocketSession> sessions = new ConcurrentHashMap<>();
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        sessions.put(session.getId(), session);
        System.out.println("Connected: " + session.getId());
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        sessions.remove(session.getId());
        System.out.println("Disconnected: " + session.getId());
    }

    public void sendMessageToAll(Integer floorId) {
        Floor floorUpdate=floorDao.findById(floorId).orElse(null);
        Integer shelfId=floorUpdate.getShelfId();
        Shelf shelf=shelfDao.findById(shelfId).orElse(null);
        List<Shelf> shelfList=shelfDao.getShelvesByRepositoryName(shelf.getRepositoryName());
        List<Floor> floorList=floorDao.getFloorsByShelfId(shelfId);
        JSONArray floorsInfo=new JSONArray();
        for (Floor floor : floorList) {
            JSONObject floorJSONObject = new JSONObject();
            floorJSONObject.put("floorOrder",floor.getFloorOrder());
            floorJSONObject.put("status",floor.getStatus());
            if(Integer.valueOf(floor.getStatus())!=1){
                Inbound inbound = inboundDao.findById(floor.getInboundId()).orElse(null);
                floorJSONObject.put("userId",inbound.getCreateUser());
                floorJSONObject.put("name",inbound.getName());
                floorJSONObject.put("companyId",inbound.getCompanyId());
            }
            floorsInfo.add(floorJSONObject);
        }
        JSONObject messageJSON=new JSONObject();
        messageJSON.put("repositoryName",shelf.getRepositoryName());
        messageJSON.put("shelfId",shelfId);
        messageJSON.put("shelvesInfo",shelfList);
        messageJSON.put("floorsInfo",floorsInfo);
        String jsonString = JSON.toJSONString(messageJSON);
        System.out.println(jsonString);
        TextMessage textMessage = new TextMessage(jsonString);
        sessions.forEach((id, session) -> {
            try {
                if (session.isOpen()) {
                    session.sendMessage(textMessage);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

}
